﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using Fitment.App_Code;


namespace Fitment
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class PopWinHandler : IHttpHandler
    {
        string action = "";
        string HTMLSTR = "";

        Data_Pop dc_popwin = new Data_Pop();  //获取要查询的语句
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            response.ContentType = "text/html";   //设置报文格式
            action = request.Params["action"];   //用于获得标识
            codeHandle(action, request, response);  //调用相关函数
        }

        /// <summary>
        /// 获取传输的参数
        /// </summary>
        /// <param name="action"></param>
        /// <param name="request"></param>
        /// <param name="response"></param>
        private void codeHandle(string action, HttpRequest request, HttpResponse response)
        {
            switch (action)
            {
                case "getpopdata":    //获取弹出框数据
                    getData(request, response);
                    break;
                case "":
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 用于接收传入数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        private void getData(HttpRequest request, HttpResponse response)
        {
            

            int userID=Convert.ToInt32(request.Cookies["userID"].Value);  //获取用户ID
            getDAData(userID);   //获取用户需要解决的工程
            getUIData(userID);    //获取用户信息
            getPTData(userID);   //获取用户总的工程数

            response.Write(HTMLSTR);   //请求的数据返回


        }

        /// <summary>
        ///用于获取需要提前项目信息
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        private string getDAData(int usercode)
        {
            DataTable _tb = dc_popwin.getHouse(usercode);
            if (_tb != null && _tb.Rows.Count > 0)
            {
                HTMLSTR += "<fieldset><legend>亟待解决延迟工程列表</legend>";

                for (int i = 0; i < _tb.Rows.Count; i++)
                {
                    DataTable _dt = dc_popwin.getDelay(Convert.ToInt32(_tb.Rows[i][0]));
                    string strNum = String.Format("施工项目{0}:", i + 1);   //用于表示项目的编号
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        string strUrl = String.Format("<a href=\"../ShowDelay.aspx?delay_id={0}\"><<现在处理</a>", _dr["Pro_ID"]);
                        HTMLSTR += "<li style='color: #FF0000'>" + strNum + _dr["ProjectName"] + "开始时间:&nbsp;&nbsp;" + _dr["StartDate"] + strUrl + "</li>";
                    }


                }
                HTMLSTR += "</fieldset>";
            }


            return HTMLSTR;

        }

        /// <summary>
        /// 显示用户所有装修工程
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        private string getPTData(int usercode)
        {
            DataTable _tb = dc_popwin.getHouse(usercode);
            string strCount;
            if (_tb != null && _tb.Rows.Count > 0)
            {
                
  
                    strCount = String.Format("<fieldset><legend>装修工程房子数:{0}</legend>", _tb.Rows.Count);
                    HTMLSTR += strCount;
                    for (int i = 0; i < _tb.Rows.Count; i++)
                    {
                        strCount = String.Format("<li> 房子{0}:", i + 1);
                        HTMLSTR += strCount;
                        DataTable _dt = dc_popwin.getProject(Convert.ToInt32(_tb.Rows[i][0]));   //获得工程
                        int hid = Convert.ToInt32(_tb.Rows[i][0]);
                        MyLinqDataContext data = new MyLinqDataContext();
                        var date = from p in data.ProjectInfo where p.House_ID == hid && Convert.ToDateTime(p.StartDate) != null select p.StartDate;
                        if (date.Count() == 0)
                        {
                            HTMLSTR += "施工项目" + _dt.Rows[0][0] + "项还没有预定时间哦<a href=\"../User/myHouse.aspx\" style='color: #FF0000'>现在预定！</a></li>";
                        }
                        else
                        {
                            string strSelect = String.Format("<a href=\"../ShowProject.aspx?id={0}\">查看详情</a>", _tb.Rows[i][0]);
                            HTMLSTR += "施工项目" + _dt.Rows[0][0] + "项&nbsp;&nbsp;" + strSelect + "</li>";
                        }
                    }
                        
         
                
            }else
                
                {
                    strCount = String.Format("<fieldset><legend>装修工程房子数:{0}</legend>", _tb.Rows.Count);
                    HTMLSTR += strCount + "<li style='color: #FF0000'>你还没有装修记录哦&nbsp;&nbsp;<a href =\"../User/houseInfo.aspx\">现在开始装修</a></li>"; ;
                }
            HTMLSTR += "</fieldset>";
            return HTMLSTR;
        }

        /// <summary>
        /// 获取用户个人信息
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="jbbz"></param>
        /// <returns></returns>
        private string getUIData(int usercode)
        {
            DataTable _dt = dc_popwin.getUserInfo(usercode);
            if (_dt != null && _dt.Rows.Count > 0)
            {
                HTMLSTR += "<fieldset><legend>用户名</legend>";
                foreach (DataRow _dr in _dt.Rows)
                {
                    HTMLSTR += "<li>" + _dr["Name"] + "&nbsp;&nbsp;<a href='../User/userInfo.aspx'>查看信息</a></li>";
                }
                HTMLSTR += "</fieldset>";
            }
            return HTMLSTR;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
